﻿using Microsoft.Phone.BackgroundAudio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TheVerge.Base.Models;
using TheVerge.Base.Platform;
using TheVerge.Phone.Controls;

namespace TheVerge.Phone.Imp
{
    public class PhoneAudioPlayer : IAudioPlayer
    {

        private const string ARTIST = @"The Verge";
        private const string ALBUM = @"Podcasts";

        private static AudioTrack GetTrack() { return BackgroundAudioPlayer.Instance.Track; }

        public PhoneAudioPlayer()
        {
            BackgroundAudioPlayer.Instance.PlayStateChanged += Instance_PlayStateChanged;
        }

        public TimeSpan Duration
        {
            get
            {
                var track = GetTrack();
                if (track == null) return TimeSpan.Zero;
                return track.Duration;
            }
        }

        public TimeSpan Position
        {
            get { return BackgroundAudioPlayer.Instance.Position; }
        }

        public AudioPlayState CurrentState { get { return (AudioPlayState)(int)BackgroundAudioPlayer.Instance.PlayerState; } }

        public bool IsPlaying()
        {
            return BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing;
        }

        public bool IsPlaying(VMPodcast podcastEntry)
        {
            return IsPlaying() && IsActiveTrack(podcastEntry);
        }

        public bool IsPlayingPodcast()
        {
            return IsPlaying() && IsVergeTrack(GetTrack());
        }

        public bool IsActiveTrack(VMPodcast podcastEntry)
        {
            if (podcastEntry == null) return false;

            var track = GetTrack();

            return IsVergeTrack(track) && track.Title == podcastEntry.Title;
        }

        public void FastForward() { BackgroundAudioPlayer.Instance.FastForward(); }

        public void Pause() { BackgroundAudioPlayer.Instance.Pause(); }

        public void Play(VMPodcast podcast)
        {
            if (!IsActiveTrack(podcast))
            {
                var track = new AudioTrack(new Uri(podcast.Mp3Link, UriKind.Absolute), podcast.Title, ARTIST, ALBUM, null);
                BackgroundAudioPlayer.Instance.Track = track;
            }

            BackgroundAudioPlayer.Instance.Play();
        }

        public void Rewind() { BackgroundAudioPlayer.Instance.Rewind(); }

        public bool Seek(TimeSpan position)
        {
            if (BackgroundAudioPlayer.Instance.CanSeek)
            {
                try
                {
                    BackgroundAudioPlayer.Instance.Position = position;
                    return true;
                }
                catch { }
            }

            return false;
        }

        public event EventHandler<AudioPlayStateChangedEventArgs> PlayStateChanged = delegate { };

        private void Instance_PlayStateChanged(object sender, EventArgs e)
        {
            var args = new AudioPlayStateChangedEventArgs();
            args.CurrentState = CurrentState;
            PlayStateChanged(this, args);
        }

        private static bool IsVergeTrack(AudioTrack track)
        {
            if (track == null) return false;
            return track.Album == ALBUM && track.Artist == ARTIST;
        }
    }
}
